#include "StdAfx.h"
#include "AmplitudeIndex.h"
#include "Complex.h"

AmplitudeIndex::~AmplitudeIndex(void)
{
}

AmplitudeIndex::AmplitudeIndex(double amplitude, unsigned int index)
{
	this->amplitude = amplitude;
	this->index = index;
}

SortedAmplitudes::SortedAmplitudes(Complex* complex, unsigned int length)
{
	for(unsigned int i=0; i<length/2; i++)
	{
		double d = complex[i].r;
		if(d<0)
			d = -d;//abs not working with doubles?

		//use -d as index because the map is sorted from smallest to largest
		std::vector<AmplitudeIndex>* curVec = NULL;
		if(map.find(-d)==map.end())
		{
			curVec = new std::vector<AmplitudeIndex>();
			map[-d] = curVec;
		}		
		else
		{
			curVec = map[-d];
		}
		curVec->push_back(AmplitudeIndex(d, i));
	}
}

SortedAmplitudes::~SortedAmplitudes()
{
	std::map<double, std::vector<AmplitudeIndex>* >::iterator it = map.begin();
	while(it != map.end())
	{
		std::vector<AmplitudeIndex>* curVec = it->second;
		delete it->second;
		it++;
	}
}

SortedAmplitudeMap* SortedAmplitudes::GetSortedAmplitudeMap()
{
	return &map;
}

void SortedAmplitudes::GetHighestAmplitudes(unsigned int count, std::vector<AmplitudeIndex>& destination)
{
	//std::map<double, std::vector<AmplitudeIndex>* >::iterator it = map.begin();
	SortedAmplitudeMap::iterator it = map.begin();
	for(unsigned int i=0; i<map.size() && destination.size()<count; i++, it++)
	{
		std::vector<AmplitudeIndex>* curVec = it->second;
		for(unsigned int j=0; j<curVec->size() && destination.size()<count; j++)
		{
			if(curVec->at(j).amplitude>0.0)
				destination.push_back(curVec->at(j));	
		}
	}
}

